#!/bin/sh

DHCP_MAIN_START=
DHCP_MAIN_END=
DHCP_MAIN_LIMIT=
DHCP_MAIN_DHCPLEASE=
DHCP_MAIN_DNS=
DHCP_MAIN_NETMASK=
DHCP_MAIN_IGNORE=

DHCP_VICE_1_START=
DHCP_VICE_1_END=
DHCP_VICE_1_LIMIT=
DHCP_VICE_1_DHCPLEASE=
DHCP_VICE_1_DNS=
DHCP_VICE_1_NETMASK=
DHCP_VICE_1_IGNORE=

DHCP_VICE_2_START=
DHCP_VICE_2_END=
DHCP_VICE_2_LIMIT=
DHCP_VICE_2_DHCPLEASE=
DHCP_VICE_2_DNS=
DHCP_VICE_2_NETMASK=
DHCP_VICE_2_IGNORE=

FORM_CONFIG_STATION=/tmp/form_config/
DHCP_CONFIG_LOAD_FILE=${FORM_CONFIG_STATION}/dhcp
FACTORY_CONFIG_FILE=/etc/tozed_param

mkdir -p ${FORM_CONFIG_STATION}
cat ${FACTORY_CONFIG_FILE} | grep TZDHCP_ > ${DHCP_CONFIG_LOAD_FILE}
chmod 777 ${DHCP_CONFIG_LOAD_FILE}
. ${DHCP_CONFIG_LOAD_FILE}

if [ "${TZDHCP_MAIN_ENABLE}" == "n" ]; then
    DHCP_MAIN_IGNORE=1
else
	DHCP_MAIN_IGNORE=0
fi

if [ "${TZDHCP_VICE_1_ENABLE}" == "n" ]; then
    DHCP_VICE_1_IGNORE=1
else
	DHCP_VICE_1_IGNORE=0
fi

if [ "${TZDHCP_VICE_2_ENABLE}" == "n" ]; then
    DHCP_VICE_2_IGNORE=1
else
	DHCP_VICE_2_IGNORE=0
fi

if [ "${TZDHCP_MAIN_DHCPSTART}" != "" ]; then
	DHCP_MAIN_START=`echo ${TZDHCP_MAIN_DHCPSTART} | awk -F '.' '{print $4}'`
else
	DHCP_MAIN_START=100
fi

if [ "${TZDHCP_MAIN_DHCPEND}" != "" ]; then
	DHCP_MAIN_END=`echo ${TZDHCP_MAIN_DHCPEND} | awk -F '.' '{print $4}'`
else
	DHCP_MAIN_END=200
fi

DHCP_MAIN_LIMIT=`expr ${DHCP_MAIN_END} - ${DHCP_MAIN_START} + 1`

if [ "${TZDHCP_MAIN_DHCPLEASE}" != "" ]; then
	DHCP_MAIN_DHCPLEASE="${TZDHCP_MAIN_DHCPLEASE}h"
else
	DHCP_MAIN_DHCPLEASE=24h
fi

if [ "${TZDHCP_MAIN_DNS}" != "" ]; then
	DHCP_MAIN_DNS="${TZDHCP_MAIN_DNS}"
else
	if [ "${TZDHCP_MAIN_IP}" = "" ]; then
		DHCP_MAIN_DNS=192.168.2.1
	else
		DHCP_MAIN_DNS=${TZDHCP_MAIN_IP}
	fi
fi

if [ "${TZDHCP_MAIN_NETMASK}" != "" ]; then
	DHCP_MAIN_NETMASK="${TZDHCP_MAIN_NETMASK}"
else
	DHCP_MAIN_NETMASK=255.255.255.0
fi


if [ "${TZDHCP_VICE_1_DHCPSTART}" != "" ]; then
	DHCP_VICE_1_START=`echo ${TZDHCP_VICE_1_DHCPSTART} | awk -F '.' '{print $4}'`
else
	DHCP_VICE_1_START=100
fi

if [ "${TZDHCP_VICE_1_DHCPEND}" != "" ]; then
	DHCP_VICE_1_END=`echo ${TZDHCP_VICE_1_DHCPEND} | awk -F '.' '{print $4}'`
else
	DHCP_VICE_1_END=200
fi

DHCP_VICE_1_LIMIT=`expr ${DHCP_VICE_1_END} - ${DHCP_VICE_1_START} + 1`

if [ "${TZDHCP_VICE_1_DHCPLEASE}" != "" ]; then
	DHCP_VICE_1_DHCPLEASE="${TZDHCP_VICE_1_DHCPLEASE}h"
else
	DHCP_VICE_1_DHCPLEASE=24h
fi

if [ "${TZDHCP_VICE_1_DNS}" != "" ]; then
	DHCP_VICE_1_DNS="${TZDHCP_VICE_1_DNS}"
else
	if [ "${TZDHCP_VICE_1_IP}" = "" ]; then
		DHCP_VICE_1_DNS=192.168.3.1
	else
		DHCP_VICE_1_DNS=${TZDHCP_VICE_1_IP}
	fi
fi

if [ "${TZDHCP_VICE_1_NETMASK}" != "" ]; then
	DHCP_VICE_1_NETMASK="${TZDHCP_VICE_1_NETMASK}"
else
	DHCP_VICE_1_NETMASK=255.255.255.0
fi


if [ "${TZDHCP_VICE_2_DHCPSTART}" != "" ]; then
	DHCP_VICE_2_START=`echo ${TZDHCP_VICE_2_DHCPSTART} | awk -F '.' '{print $4}'`
else
	DHCP_VICE_2_START=100
fi

if [ "${TZDHCP_VICE_2_DHCPEND}" != "" ]; then
	DHCP_VICE_2_END=`echo ${TZDHCP_VICE_2_DHCPEND} | awk -F '.' '{print $4}'`
else
	DHCP_VICE_2_END=200
fi

DHCP_VICE_2_LIMIT=`expr ${DHCP_VICE_2_END} - ${DHCP_VICE_2_START} + 1`

if [ "${TZDHCP_VICE_2_DHCPLEASE}" != "" ]; then
	DHCP_VICE_2_DHCPLEASE="${TZDHCP_VICE_2_DHCPLEASE}h"
else
	DHCP_VICE_2_DHCPLEASE=24h
fi

if [ "${TZDHCP_VICE_2_DNS}" != "" ]; then
	DHCP_VICE_2_DNS="${TZDHCP_VICE_2_DNS}"
else
	if [ "${TZDHCP_VICE_2_IP}" = "" ]; then
		DHCP_VICE_2_DNS=192.168.4.1
	else
		DHCP_VICE_2_DNS=${TZDHCP_VICE_2_IP}
	fi
fi

if [ "${TZDHCP_VICE_2_NETMASK}" != "" ]; then
	DHCP_VICE_2_NETMASK="${TZDHCP_VICE_2_NETMASK}"
else
	DHCP_VICE_2_NETMASK=255.255.255.0
fi

cat > /etc/config/dhcp <<EOF

config dnsmasq
        option domainneeded '1'
        option boguspriv '1'
        option filterwin2k '0'
        option localise_queries '1'
        option rebind_protection '1'
        option rebind_localhost '1'
        option local '/lan/'
        option domain 'lan'
        option expandhosts '1'
        option nonegcache '0'
        option authoritative '1'
        option readethers '1'
        option leasefile '/tmp/dhcp.leases'
        option resolvfile '/tmp/resolv.conf.auto'
        option localservice '1'

config dhcp 'lan'
        option interface 'lan'
        option start '${DHCP_MAIN_START}'
        option limit '${DHCP_MAIN_LIMIT}'
        option leasetime '${DHCP_MAIN_DHCPLEASE}'
        option ipv6mode 'stateless'
        option ra_management '0'
        option dhcpv6 'server'
        option ra 'server'
        option netmask '${DHCP_MAIN_NETMASK}'
        option main_dns '${DHCP_MAIN_DNS}'
        option vice_dns ''
        option ignore '${DHCP_MAIN_IGNORE}'

config dhcp 'lan1'
        option interface 'lan1'
        option start '${DHCP_VICE_1_START}'
        option limit '${DHCP_VICE_1_LIMIT}'
        option leasetime '${DHCP_VICE_1_DHCPLEASE}'
        option ipv6mode 'stateless'
        option ra_management '0'
        option dhcpv6 'server'
        option ra 'server'
        option netmask '${DHCP_VICE_1_NETMASK}'
        option main_dns '${DHCP_VICE_1_DNS}'
        option vice_dns ''
        option ignore '${DHCP_VICE_1_IGNORE}'

config dhcp 'lan2'
        option interface 'lan2'
        option start '${DHCP_VICE_2_START}'
        option limit '${DHCP_VICE_2_LIMIT}'
        option leasetime '${DHCP_VICE_2_DHCPLEASE}'
        option ipv6mode 'stateless'
        option ra_management '0'
        option dhcpv6 'server'
        option ra 'server'
        option netmask '${DHCP_VICE_2_NETMASK}'
        option main_dns '${DHCP_VICE_2_DNS}'
        option vice_dns ''
        option ignore '${DHCP_VICE_2_IGNORE}'

config dhcp 'wan'
        option interface 'wan'
        option ignore '1'

config odhcpd 'odhcpd'
        option maindhcp '0'
        option leasefile '/tmp/hosts/odhcpd'
        option leasetrigger '/usr/sbin/odhcpd-update'

EOF
